#include "BatteryModel.h"
#include "Common/DataStructureMacro.h"
#include "ssas_api.h"

typedef enum
{
    ALWAYS_OFF,
    ALWAYS_ON,
    ON_AND_POPUP,
} MachineState;

static MachineState currentStatus;
static uint16_t     rawValue;
static Option_U8    showFlag;
static uint8_t      widget_visible_mask = 0;

static void init()
{
    currentStatus = ALWAYS_OFF;
    rawValue = 0;
    showFlag.value_ = false;
    showFlag.update_ = false;
}

static MachineState getNewState()
{
    MachineState newStatus = ALWAYS_ON;
    do
    {
        if(!isFinishSelfTest(SELF_TEST_3S))
        {
            newStatus = ALWAYS_ON;
            break;
        }

        {
            int ret = get_message_state(COM_IC_BMS_GENERAL_STATUS_2);
            if(COM_MSG_INIT == ret || COM_MSG_TIMEOUT == ret)
            {
                newStatus = ALWAYS_ON;
                break;
            }
        }
        

        // 36A CheckSum计算错误 todo
        // newStatus = ALWAYS_ON;

        // READY
        bool isReady = true;

        // 信号有效性

        {
            Std_ReturnType ret = Com_ReceiveSignal(COM_SID_BatSOC, &rawValue);
            if(E_NOT_OK == ret)
            {
                newStatus = ALWAYS_ON;
                break;
            }
            bool isLowPower = (rawValue*0.1) <= 20.0f;
            
            if(!isLowPower)
            {
                newStatus = ALWAYS_OFF;
            }
            else 
            {
                if (isReady)
                {
                    newStatus = ON_AND_POPUP;
                }
                else
                {
                    newStatus = ALWAYS_ON;
                }
            }
            break;
        }
    } while (false);

    return newStatus;
}

static void handlStateTransition(MachineState newStatus)
{
    if(currentStatus != newStatus)
    {
        switch (currentStatus)
        {            
            case ON_AND_POPUP:
            {
                //stop popup todo
                break;
            }

            default:
                break;
        }

        currentStatus = newStatus;

        switch (currentStatus)
        {            
            case ON_AND_POPUP:
            {
                //show popup todo
                break;
            }

            default:
                break;
        }

    }
}

static void postprocess()
{
    switch (currentStatus)
    {
        case ALWAYS_OFF:
        {
            if(showFlag.value_ == ON)
            {
                showFlag.value_ = OFF;
                showFlag.update_ = true;
            }
            break;
        }

        case ALWAYS_ON:
        case ON_AND_POPUP:
        {
            if(showFlag.value_ == OFF)
            {
                showFlag.value_ = ON;
                showFlag.update_ = true;
            }
            break;
        }
        
        default:
            break;
    }
}

static void run()
{
    MachineState newStatus = getNewState();
    handlStateTransition(newStatus);
    postprocess();
}

static void release()
{

}

BatteryModel g_BatteryModelInstance = {
    {
        init,
        run,
        release,
        MIN_PERIOD,
    },
};

// ----------------------------------View------------------------------
void refresh_tt1View(ItuElement* elememt, bool isUnconditional)
{
    if(showFlag.update_)
    {
        ituCheckBoxSetChecked((ITUCheckBox*)elememt->widget_, showFlag.value_);
        showFlag.update_ = false;
    }
}

void reporting_self_validity_tt1View(ItuElement* elememt)
{
    uint8_t is_visible = (elememt && elememt->isVaild_) ? 0x01 : 0x00;
    widget_visible_mask <<= 1;
    widget_visible_mask |= is_visible;
}